import os.path

tab = [ [" "," "," "],[" "," "," "],[" "," "," "] ]
File = open('/tmp/contest.txt', 'a')
contest = None
wins = None
path = "/tmp/contest.txt"






def map_screen():
    return '''
    A   B   C
  -------------
1 | %s | %s | %s |
  -------------
2 | %s | %s | %s |
  -------------
3 | %s | %s | %s |
  -------------''' % (tab[0][0], tab[0][1], tab[0][2], tab[1][0], tab[1][1], tab[1][2], tab[2][0], tab[2][1], tab[2][2])


def get_value(player):

    while True:
        print "%s plays" % player

        reply_h = raw_input("enter column:")
        reply_w = raw_input("enter row(Use a b or c):")

   
        if not reply_h.isdigit():
            print "entered inproper coordinetes, please use total numbers only"
   
        elif not reply_w in ('a','b','c'):
            print "entered inproper coordinetes, a b or c"

        else:
       
            reply_h = int(reply_h) - 1
            
            if reply_w == 'a':
                reply_w = 0

            if reply_w == 'b':
                reply_w = 1

            if reply_w == 'c':
                reply_w = 2 

            if not (reply_h >= 0 and reply_h <= 2 and reply_w >= 0 and reply_w <= 2):
                print "please use numbers from 1 to 3"

            elif tab[reply_h][reply_w] !=" ":
                print "this field is already filled in"

            else:
                tab[reply_h][reply_w] = player
                break



def contest_check(P):

    contest = None

    if (tab[0][0] == P and tab[0][1] == P and tab[0][2] == P):
        contest = "player %s wins!" % P 

    if (tab[1][0] == P and tab[1][1] == P and tab[1][2] == P):
        contest = "player %s wins!" % P
   
    if (tab[2][0] == P and tab[2][1] == P and tab[2][2] == P):
        contest = "player %s wins!" % P

    if (tab[0][0] == P and tab[1][0] == P and tab[2][0] == P):
        contest = "player %s wins!" % P

    if (tab[0][1] == P and tab[1][1] == P and tab[2][1] == P):
        contest = "player %s wins!" % P

    if (tab[0][2] == P and tab[1][2] == P and tab[2][2] == P):
        contest = "player %s wins!" % P

    if (tab[2][0] == P and tab[1][1] == P and tab[0][2] == P):
        contest = "player %s wins!" % P

    if (tab[0][0] == P and tab[1][1] == P and tab[2][2] == P):
        contest = "player %s wins!" % P

    elif (tab[0][0] != " " and tab[0][1] != " " and tab[0][2] != " " and tab[1][0] != " " and tab[1][1] != " " and tab[1][2] != " " and tab[2][0] != " " and tab[2][1] != " " and tab[2][2] != " "):
        contest = "draw!"

    return contest
def stats_check():

    if not os.path.exists(path):
        open(path, "a").write("0,0,0")

    File = open(path, "r")
    save = File.read()

    save = save.split(",")    

    
    if contest_check("X") != None or contest_check("O") != None:       
        
            if contest_check("X") == "player X wins!":        
                print "Player X Wins!"
                save[0] = str( int(save[0])+ 1 )

            if contest_check("O") == "player O wins!":       
                print "Player O Wins!"
                save[1] = str( int(save[1])+ 1 )

            if contest_check("X") == "draw!":          
                print "Draw"
                save[2] = str( int(save[2])+ 1 )
            
    
 

    print "Wins_X: %s  Wins_O: %s Draws: %s" % ( save[0], save[1], save[2] )

    save = ",".join(save)
    File.close()



    File_new = open(path, "w")
    File_new.write(save)


    File_new.close




while True:

    print map_screen()

    get_value('X')

 
    if contest_check("X") != None:
        print map_screen()
        print contest_check("X")   
        print stats_check()
        break
    
    print map_screen()

    get_value('O')

    if contest_check('O') != None:
        print map_screen()
        print contest_check("O")
        print stats_check()
        break

        
    